package com.deluxelx.soul.inn.web.mine;

import cn.hutool.core.collection.CollectionUtil;
import com.deluxelx.soul.inn.bean.article.ArticleVO;
import com.deluxelx.soul.inn.bean.counselor.AppointmentVO;
import com.deluxelx.soul.inn.bean.counselor.CounselorFollowVO;
import com.deluxelx.soul.inn.bean.course.CourseVO;
import com.deluxelx.soul.inn.bean.question.QuestionVO;
import com.deluxelx.soul.inn.bean.user.UserInfoDTO;
import com.deluxelx.soul.inn.bean.user.UserInfoVO;
import com.deluxelx.soul.inn.common.result.ResponseCode;
import com.deluxelx.soul.inn.common.result.SoulInnResult;
import com.deluxelx.soul.inn.service.article.ArticleService;
import com.deluxelx.soul.inn.service.counselor.CounselorService;
import com.deluxelx.soul.inn.service.course.CourseService;
import com.deluxelx.soul.inn.service.question.QuestionService;
import com.deluxelx.soul.inn.service.user.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * 前端页面：个人中心
 *
 * @author: zihao.liao
 * @date: 2024/1/24 15:47
 */
@RestController
@RequestMapping("/soul/inn/mine")
@Slf4j
public class MineController {

    @Resource
    private UserInfoService userInfoService;

    @Resource
    private CounselorService counselorService;

    @Resource
    private ArticleService articleService;

    @Resource
    private CourseService courseService;

    @Resource
    private QuestionService questionService;

    @PostMapping("/myInfo")
    public SoulInnResult<UserInfoVO> getMyInfo(@RequestBody UserInfoDTO userInfoDTO) {
        Optional<UserInfoVO> userInfoVO = userInfoService.fetchUserInfo(userInfoDTO.getUserId());
        return userInfoVO.map(SoulInnResult::success)
                .orElseGet(() -> SoulInnResult.fail("获取个人信息失败"));
    }

    @PostMapping("/myFollowCounselor")
    public SoulInnResult<List<CounselorFollowVO>> getMyFollowCounselor(@RequestBody UserInfoDTO userInfoDTO) {
        List<CounselorFollowVO> counselorVOList = counselorService.fetchMyFollowCounselorList(userInfoDTO.getUserId());
        if (CollectionUtil.isNotEmpty(counselorVOList)) {
            return SoulInnResult.success(counselorVOList);
        }
        return SoulInnResult.fail(ResponseCode.COUNSELOR_NOT_FOLLOW);
    }

    @PostMapping("/myFollowArticleList")
    public SoulInnResult<List<ArticleVO>> getMyFollowArticle(@RequestBody UserInfoDTO userInfoDTO) {
        List<ArticleVO> articleVOS = articleService.fetchMyFollowArticle(userInfoDTO.getUserId());
        if (CollectionUtil.isNotEmpty(articleVOS)) {
            return SoulInnResult.success(articleVOS);
        }
        return SoulInnResult.fail("未收藏任何文章");
    }

    @PostMapping("/myFollowCourseList")
    public SoulInnResult<List<CourseVO>> getMyFollowCourse(@RequestBody UserInfoDTO userInfoDTO) {
        List<CourseVO> courseVOS = courseService.fetchMyFollowCourse(userInfoDTO.getUserId());
        if (CollectionUtil.isNotEmpty(courseVOS)) {
            return SoulInnResult.success(courseVOS);
        }
        return SoulInnResult.fail("未收藏任何课程");
    }

    @PostMapping("/myFollowQuestionList")
    public SoulInnResult<List<QuestionVO>> getMyFollowQuestion(@RequestBody UserInfoDTO userInfoDTO) {
        List<QuestionVO> questionVOS = questionService.fetchMyFollowQuestionList(userInfoDTO.getUserId());
        if (CollectionUtil.isNotEmpty(questionVOS)) {
            return SoulInnResult.success(questionVOS);
        }
        return SoulInnResult.fail("未点赞任何问答");
    }

    @GetMapping("/isCounselor")
    public SoulInnResult<Boolean> isCounselor(String userId) {
        return SoulInnResult.success(counselorService.isCounselor(userId));
    }

    @PostMapping("/changeUserInfo")
    public SoulInnResult<Boolean> changeUserInfo(@RequestBody UserInfoDTO userInfoDTO) {
        try {
            return SoulInnResult.success(userInfoService.modifyUserInfo(userInfoDTO));
        } catch (Exception e) {
            return SoulInnResult.fail("修改个人信息失败");
        }
    }

    @GetMapping("/myAppointmentList")
    public SoulInnResult<List<AppointmentVO>> fetchMyAppointmentList(String userId, Integer status) {
        List<AppointmentVO> appointmentVOS = counselorService.fetchMyAppointmentList(userId, status);
        if (CollectionUtil.isNotEmpty(appointmentVOS)) {
            return SoulInnResult.success(appointmentVOS);
        }
        return SoulInnResult.fail("咨询列表为空");
    }

    @PostMapping("/changeUserAvatar")
    public SoulInnResult<Boolean> changeUserAvatar(MultipartFile file, String userId) {
        try {
            return SoulInnResult.success(userInfoService.uploadAvatar(file, userId));
        } catch (Exception e) {
            log.error(e.getMessage());
            return SoulInnResult.fail("修改头像失败");
        }
    }
}
